package com.ihrm.company.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.PostUpdate;
import java.util.List;

/**
 * @auther chongxiongwei
 * @create 2020-04-14-21:39
 */
@CrossOrigin
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    // 保存企业
    @PostMapping("")
    public Result save(@RequestBody Company company){
        companyService.addCompany(company);
        return new Result(ResultCode.SUCCESS);
    }

    // 根据ID更新企业
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") String id,@RequestBody Company company){
        company.setId(id);
        companyService.update(company);
        return new Result(ResultCode.SUCCESS);
    }

    // 根据ID删除企业
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        companyService.delete(id);
        return new Result(ResultCode.SUCCESS);
    }

    // 根据ID查找企业
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id){
        Company company = companyService.findById(id);
        return new Result(ResultCode.SUCCESS,company);
    }

    // 查询全部企业列表
    @GetMapping("")
    public Result findAll(){
        List<Company> companies = companyService.findAll();
        return new Result(ResultCode.SUCCESS,companies);
    }
}
